package com.yinhai.message.core.exception;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yinhai.core.domain.api.domain.service.ITimeService;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.core.bpo.IMessageBpo;
import com.yinhai.message.core.po.MessageDetailPo;
import com.yinhai.message.core.po.id.MessageDetailId;
import com.yinhai.message.core.util.IMessageConstant;

@Service
public class ResultsHandle {

	@Resource(name = "messageBpo")
	private IMessageBpo messageBpo;
	
	@Resource(name = "timeAdaptedService")
	protected ITimeService timeAdaptedService;
	
	public void handleResults(List<TMUser> list ,String mgid, String messageType){
		List<MessageDetailPo> poList = new ArrayList<>();
		Date senddate = timeAdaptedService.getSysDate();
		for(TMUser user: list){
			String appid = user.getAppId();
			String userid = user.getUserId();
			MessageDetailPo statePo = new MessageDetailPo();
			MessageDetailId stateId = new MessageDetailId();
			stateId.setMgid(mgid);
			stateId.setUserid(userid);
			stateId.setSystemid(appid);
			stateId.setSendertype(messageType);
			statePo.setId(stateId);
			statePo.setSenddate(senddate);
			statePo.setState(user.getSendSign());
			statePo.setErrorinfo(user.getErrorInfo());
			poList.add(statePo);
//			messageBpo.modifyMessageSate(statePo);
		}
		
		if(messageType.equals(MessageType.WECHAT)){
			MessageDetailPo statePo = new MessageDetailPo();
			MessageDetailId stateId = new MessageDetailId();
			stateId.setMgid(mgid);
			stateId.setUserid(IMessageConstant.WX);
			stateId.setSystemid(IMessageConstant.WX);
			stateId.setSendertype(messageType);
			statePo.setId(stateId);
			statePo.setSenddate(senddate);
			statePo.setState(IMessageConstant.MSG_STATE_SEND_ALREADY);
			poList.add(statePo);
//			messageBpo.modifyMessageSate(statePo);
		}
		messageBpo.modifyMessageSateBatch(poList);
	}
}
